/*******************************************************************************
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 *
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.core.internal;

import com.google.common.base.Preconditions;

import net.sourceforge.perforce.core.IOpenValidator;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.team.FileModificationValidationContext;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;

/**
 * @version $Revision: 494 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
class FileModificationValidator extends org.eclipse.core.resources.team.FileModificationValidator {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the associated team provider. */
  private final P4TeamProvider provider;

  // -------------------------------------------------------------------------
  // Public Constructors

  /**
   * Constructor for FileModificationValidator.
   *
   * @param provider the associated P4TeamProvider instance.
   */
  public FileModificationValidator(P4TeamProvider provider) {
    this.provider = Preconditions.checkNotNull(provider);
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public IStatus validateSave(IFile file) {
    IStatus result = new Status(IStatus.OK, PerforceCorePlugin.ID, IStatus.OK, "", null);
    if (!file.isReadOnly()) {
      return result;
    }

    if (provider.hasRemote(file)) {
      try {
        if (!provider.isCheckedOut(file)) {
          provider.openForEdit(new IResource[] { file }, IResource.DEPTH_ZERO, null);
        }
      } catch (PerforceException e) {
        result = e.getStatus();
      }
    }

    return result;
  }

  @Override
  public IStatus validateEdit(IFile[] files, FileModificationValidationContext context) {
    IOpenValidator[] validators = PerforceCorePlugin.getPlugin().getOpenValidators();
    for (IOpenValidator validator : validators) {
      IStatus status = validator.validateEdit(files, context);
      if (!status.isOK()) {
        return status;
      }
    }

    try {
      provider.openForEdit(files, IResource.DEPTH_ZERO, new NullProgressMonitor());
      return new Status(Status.OK, PerforceCorePlugin.ID, 0, "", null);//$NON-NLS-1$
    } catch (PerforceException e) {
      return e.getStatus();
    }
  }
}
